<template>
	<div>
		<el-dialog :title="title" :visible.sync="dialogVisible" size="large" :close-on-press-escape="false" :before-close="off" :show-close="true" :top="dialog_margin_top">
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;margin-top:0px;margin-bottom:5px;">
				<el-form :inline="true">
					<el-form-item>
						<el-button size="small" type="warning" @click.native="openItemLink()" ><i class="fa fa-link"></i> 关联标的/责任</el-button>
					</el-form-item>
				</el-form>
			</el-col>

			<el-table :data="tableData" ref="multipleTable" v-loading="loading" element-loading-text="处理中......" border style="width: 100%;" :height="tableHeight" :highlight-current-row="true" @selection-change="rowSelected" @row-click="toggleRow">
			 	<el-table-column type="selection" width="55"></el-table-column>
				<el-table-column prop="riskcode" label="险种代码" width="160"></el-table-column>
				<el-table-column prop="riskcname" label="险种中文名称" show-overflow-tooltip min-width="240"></el-table-column>
				<el-table-column prop="riskename" label="险种英文名称" show-overflow-tooltip min-width="240"></el-table-column>
				<el-table-column prop="ownerriskcode" label="归属险种" width="110"></el-table-column>
				<el-table-column prop="riskStartDate" label="生效日期" width="150" :formatter="dateFormater"></el-table-column>
				<el-table-column prop="riskEndDate" label="失效日期" width="150" :formatter="dateFormater"></el-table-column>
				<!-- <el-table-column prop="validstatus" label="是否有效" width="120" :formatter="validFormater"></el-table-column> -->
			</el-table>

			<div class="block" style="float:right">
			    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			      :current-page="formData.currentPage"
			      :page-sizes="[15, 30, 45]"
			      :page-size="formData.pageSize"
			      layout="total, sizes, prev, pager, next, jumper"
			      :total="total">
			    </el-pagination>
			  </div>
			  <span slot="footer" class="dialog-footer">
			   <el-button @click="off">取 消</el-button>
			   <el-button type="danger" @click="delLink()" :disabled="isSelectNull">取消关联</el-button>
		  </span>
		</el-dialog>
		<Item2Risk v-if="showItem2RiskDialog"></Item2Risk>
	</div>

</template>

<script>
import RiskAPI from "../../../../js/production/risk/riskAPI"
import dateUtil from "../../../../common/js/dateUtil"
import Utils from"../../../../js/production/utils";
import Item2Risk from"./Item2Risk";
export default {
  components : {
  	Item2Risk
  },
  data () {
    	return {
    		formData: {
    		        currentPage: 1,
	                    pageSize: 15,
	                    riskcode: '',
	                    riskcname: '',
	                    validstatus: '1',
	                    orderBy: '',
	                    order: '',
	                    selectedRows: [],
	                    kind: {},
    		},
    		tableData: [],
    		total: 0,
		riskOptions: [],
		validOptions: [{
			label : '1-有效',
			value: '1'
		},{
			label : '0-无效',
			value: '0'
		}],
		isSortRequest: false,	//是否请求 
		showAddDialog: false,
		showItem2RiskDialog: false,
		tableHeight: 0,
		dialogVisible: true,
		dialog_margin_top: '5%',
		isSelectNull: true,
		title : '',
		loading : false,
	}
  },
  mounted() {
  	this.tableHeight = Utils.getScreenHeight()*0.6;
  	this.formData.kind = this.$parent.selectedRows[0];
  	if(typeof this.formData.kind === 'undefined') {
  		this.formData.kind = this.$parent.tempKind;
  		this.$parent.selectedRows.push(this.$parent.tempKind);
  	}
  	this.title = '【条款-' + this.formData.kind.kindcode + '-' + this.formData.kind.kindcname + '】已关联险种如下';
  	this.queryData();
  },
computed: {
  	selectedRows : {
  		get:function() {
  			return this.formData.selectedRows;
  		},
  		set:function(newValue) {
  			this.formData.selectedRows = newValue;
  		}
  	},
  },
  watch: {
  	selectedRows(curVal, oldVal) {
  		if (curVal.length < 1) {
  			this.isSelectNull = true;
  		} else {
  			this.isSelectNull = false;
  		}
  	}
  },
  methods: {
		queryData() {
			RiskAPI.getRiskLinked(this);
		},
		delLink() {
			RiskAPI.delLink(this);
		},
		rowSelected(rows,row) {
			this.formData.selectedRows = rows;
		},
		toggleRow(row) {
			this.$refs.multipleTable.toggleRowSelection(row);
		},
		handleSizeChange(val) {
			this.formData.pageSize = val;
			this.queryData();
		},
		handleCurrentChange(val) {
			this.formData.currentPage = val;
			this.queryData();
		},
		dateFormater(row,  column) {  
			var  date  =  row[column.property];            
			if  (date  ==  undefined)  {               
				return  "";            
			}  
			return dateUtil.getDateyMd(date);
		},
		validFormater(row,  column) {  
			var  valid =  row[column.property];            
			if  (valid ==  undefined)  {               
				return  "";            
			}  
			if (valid == 1) {
				return "有效";
			}
			if (valid == 0) {
				return "无效";
			}
		},
		off() {
			this.$parent.showRiskLinkedDialog = false;
			this.$parent.selectedRows = [];
		},
		openItemLink() {
			RiskAPI.openItemLinkDialog(this);
		}
  }
}
</script>